package com.ssyc.student.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ssyc.common.base.LazyBaseFragment;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.view.MyListView;
import com.ssyc.student.R;
import com.ssyc.student.activity.StudentGrammarDetectionActivity;
import com.ssyc.student.adapter.StAnswerLvAdapter;
import com.ssyc.student.bean.GrammarDetectionInfo;
import com.ssyc.student.bean.StAnswerInfo;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 学生端--语法检测--翻译题fragment
 * Created by Kevin on 2018/5/30
 */

public class StudentGrammarTranslateFragment extends LazyBaseFragment {

    //阅读的题目内容
    private TextView tvTopic;

    //展示答案选项的ListView
    private MyListView lvAnswer;


    //展示正确的完整句子
    private TextView tvShowContent;

    //选择正确答案后需要展示的整体布局,默认不显示
    private LinearLayout llShowAnswer;


    //提供选择答案的数据集合
    private List<StAnswerInfo> answerInfos;

    //展示答案选项的列表适配器
    private StAnswerLvAdapter adapter;

    //当前题目在同题型的位置
    private int currPos;


    //通知Acitvity进行ViewPager翻页的标识
    public static String VPSCOLL = "StudentGrammarDetectionActivityVpScoll";


    //当前题目的正确答案,通过传值获取
    public String correctAnswer = "rightAnswer";

    //题目内容
    private List<GrammarDetectionInfo.DataBean.QuestionListBean.TranslationBean> datas;


    //A到Z的数据集合
    private String[] chartNums;

    //当前题目前的总体位置
    private int lastPos;

    //答案
    private TextView tv_show_content;

    //答案序号
    private TextView tv_answer_num;

    /**
     * 提供的生成当前Fragment的方法
     *
     * @param data 包含页面数据的json字符串
     * @return
     */
    public static StudentGrammarTranslateFragment newInstance(String data, int pos) {

        StudentGrammarTranslateFragment studentReadChooseAnswerFragment = new StudentGrammarTranslateFragment();
        Bundle bundle = new Bundle();
        bundle.putString("data", data);
        bundle.putInt("pos", pos);
        studentReadChooseAnswerFragment.setArguments(bundle);

        return studentReadChooseAnswerFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_student_grammar_translate;
    }

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {
        //初始化视图
        initView(view);

        showContent();

        //提供答案选项的数据
        initData();

        //设置展示答案的ListView
        initLv();
    }


    /**
     * 初始化视图
     *
     * @param view
     */
    public void initView(View view) {
        tvTopic = view.findViewById(R.id.tv_topic);
        lvAnswer = view.findViewById(R.id.lv_answer);
        tvShowContent = view.findViewById(R.id.tv_show_content);
        llShowAnswer = view.findViewById(R.id.ll_show_answer);

        tv_answer_num = view.findViewById(R.id.tv_answer_num);
    }



    /**
     * 判断是否提交
     */
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if (getUserVisibleHint()) {
            if (StudentGrammarDetectionActivity.isCommit == 1) {

                //显示答案
                llShowAnswer.setVisibility(View.VISIBLE);

                lvAnswer.setEnabled(false);
            }
        }
    }



    /**
     * 答案选项
     */
    private void initData() {
        chartNums = getContext().getResources().getStringArray(R.array.base_letters);
        //获取当前题型页面的位置
        currPos = getArguments().getInt("pos");

        //获取传递过来的页面数据
        String dataJson = getArguments().getString("data");
        //解析该Json获取页面数据
        datas = new ArrayList<>();
        datas = GsonUtil.jsonToBean(dataJson, GrammarDetectionInfo.DataBean.QuestionListBean.class).getTranslation();

        //此题型之前的题目总数
        lastPos = GsonUtil.jsonToBean(dataJson, GrammarDetectionInfo.DataBean.QuestionListBean.class).getConjunction().size();

        //答案
        correctAnswer = datas.get(currPos).getAnswer();

        //保证存放的每个位置都对应有值
//        StudentGrammarDetectionActivity.getChooseAnswerMap().put(lastPos+currPos, "");
        answerInfos = new ArrayList<>();

        for (int i=0;i<datas.get(currPos).getDetatil().size();i++){
            answerInfos.add(new StAnswerInfo(false,datas.get(currPos).getDetatil().get(i)
                    ,chartNums[i]));
        }
        //设置题目
        tvTopic.setText(datas.get(currPos).getQuestion());
        //设置答案
        tvShowContent.setText(datas.get(currPos).getAnswer());

        if(datas.get(currPos).getAnswer().equals(datas.get(currPos).getDetatil().get(0))){
            tv_answer_num.setText("A");
        }else{
            tv_answer_num.setText("B");
        }

    }

    /**
     * 设置展示选择答案的ListView
     */
    private void initLv() {
        adapter = new StAnswerLvAdapter(getContext(), answerInfos, R.layout.student_lv_answer);
        lvAnswer.setAdapter(adapter);
        lvAnswer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
                //条目的点击事件
                for (int i = 0; i < answerInfos.size(); i++) {
                    if (pos == i) {
                        answerInfos.get(pos).isSelect = true;
                    } else {
                        answerInfos.get(i).isSelect = false;
                    }
                }
                adapter.notifyDataSetChanged();

                //重新做题后需要通知提交界面的适配器数据刷新
                EventBus.getDefault().post(new BusInfo(StudentGrammarCommitFragment.HASDONE, lastPos+currPos, answerInfos.get(pos).answerFlag));

                //将选择的答案保存到Acitivity的Map中
                StudentGrammarDetectionActivity.getChooseAnswerMap().put(lastPos+currPos, answerInfos.get(pos).answerContent);

                //判断选择答案的对错,然后保存到Map中
                if (answerInfos.get(pos).answerContent.equals(correctAnswer)) {
                    //选择正确
                    StudentGrammarDetectionActivity.getAnswerMap().put(lastPos+currPos, true);
                } else {
                    //选择错误
                    StudentGrammarDetectionActivity.getAnswerMap().put(lastPos+currPos, false);
                }

                //选择答案后跳转到下一页面,EventBus通知Activity进行ViewPager的翻页操作
                EventBus.getDefault().post(new BusInfo(VPSCOLL));
            }
        });
    }

}
